<template>
  <div class="search-history">
      <van-cell title="历史记录">
        <div v-if="isDeleteShow">
          <span @click="$emit('clear-search-histories', [])">全部删除</span>
          &nbsp;&nbsp;
          <span @click="isDeleteShow = false">完成</span>
        </div>
        <van-icon
          v-else
          name="delete"
          @click="isDeleteShow = true"
        ></van-icon>
      </van-cell>
      <van-cell
        :title="item"
        v-for="(item, index) in searchHistories"
        :key="index"
        @click="onSearchItemClick(item,index)"
      >
        <van-icon
          v-show="isDeleteShow"
          name="close"
          @click="searchHistories.splice(index, 1)"
        ></van-icon>
      </van-cell>
    <!-- /历史记录 -->
  </div>
</template>

<script>
export default {
  name: 'searchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  methods: {
    onSearchItemClick (item, index) {
      // 如果是删除状态，则执行删除操作
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1)
      } else {
        // 否则执行搜索操作
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style>
</style>
